package com.ruoyi.apduParse.strategy;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import com.ruoyi.apduParse.dto.AidInfo;
import com.ruoyi.apduParse.dto.SentenceFactoryDto;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class InstallSentenceStrategy implements SentenceStrategy {

    @Override
    public String getSentenceOfapduName(SentenceFactoryDto factoryDto) {

        List<AidInfo> aidInfos = factoryDto.getAidInfos();
        if (CollectionUtil.isEmpty(aidInfos)){
            String sentence = "指令中不存在aid";
            return sentence;
        }else if (aidInfos.size() == 1){
            String sentence = "该指令是在安装{}";
            return getApduRealUse(sentence,aidInfos);
        }else if (aidInfos.size() == 2){
            String sentence = "该指令是在{}下安装{}";
            return getApduRealUse(sentence,aidInfos);
        }else if (aidInfos.size() == 3){
            String sentence = "该指令是在{}下安装{}或{}";
            return getApduRealUse(sentence,aidInfos);
        }else {
            return "无效的指令";
        }
    }

    /**
     * 拼接
     * @param sentence
     * @param aidInfos
     * @return
     */
    private String getApduRealUse(String sentence, List<AidInfo> aidInfos) {
        log.info("拼接语句：{}",sentence);

        for (AidInfo aidInfo : aidInfos) {
            String aidName = aidInfo.getAidName();
            log.info("aidName:{}", aidName);
            sentence = StringUtils.format(sentence, aidName);
        }
        log.info("已经拼接好的语句：{}",sentence);

        return sentence;
    }
}
